package com.pdg.dungeondelver.configuration;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.newdawn.slick.SlickException;
import org.newdawn.slick.util.ResourceLoader;
import org.newdawn.slick.util.xml.XMLElement;
import org.newdawn.slick.util.xml.XMLElementList;
import org.newdawn.slick.util.xml.XMLParser;


public class Themes {
	private Map<String,Theme> themes= new HashMap<String,Theme>();
	public Theme getTheme(String name){
		return themes.get(name);
	}
	private void setTheme(String name, Theme theme) {
		themes.put(name, theme);
		
	}
	private Configuration configuration;
	public Configuration getConfiguration(){
		return configuration;
	}
	
	public Themes(Configuration theConfiguration,String ref) throws ThemesLoadException{
		setConfiguration(theConfiguration);
		if(!loadFromXml(ref)){
			throw new ThemesLoadException();
		}
	}

	private void setConfiguration(Configuration theConfiguration) {
		configuration = theConfiguration;
	}
	private boolean loadFromXml(String ref) {
		XMLParser parser = new XMLParser();
		try {
			InputStream stream = ResourceLoader.getResourceAsStream(ref);
			XMLElement root = parser.parse(ref,stream);
			XMLElementList children = root.getChildren();
			for(Integer index=0;index<children.size();++index){
				XMLElement child = children.get(index);
				setTheme(child.getName(),new Theme(this,child.getContent()));
			}
			return true;
		} catch (SlickException e) {
			e.printStackTrace();
			return false;
		} catch (ThemeLoadException e) {
			e.printStackTrace();
			return false;
		}
	}

}
